package com.sa.estupendos.garena.entity.dao;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sa.estupendos.garena.entity.User;


@Repository
@Transactional
public class BaseDAO<Entity>{
	
	@PersistenceContext
	private EntityManager _entityManager = null;
	private Class<Entity> _objectClass = null;
	
	
	public BaseDAO(Class<Entity> objectClass) {
		_objectClass = objectClass;
		System.out.println("Generic DAO++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
	}
	
	
	
	public void insert(Entity entity) {
		_entityManager.persist(entity);
	}


	public void remove(Entity entity) {
		_entityManager.remove(_entityManager.merge(entity));
	}
	
	

	public Entity findByPK(Object object) {
		return _entityManager.find(_objectClass, object);
	}
	
	public ArrayList<User> findAll() {
		Query query = _entityManager.createQuery("from User");
		return (ArrayList<User>) query.getResultList();
	}

	public EntityManager getEntityManager() {
		return _entityManager;
	}
	
	public void setEntityManager(EntityManager entityManager) {
		_entityManager = entityManager;
	}
}